# -*- coding: utf-8 -*-

import src._classias_ as cls
import src._conf_     as conf
import src._perceptron_binary_ as per_bc
import src._perceptron_multi_  as per_mc
import sys
reload(sys)
sys.setdefaultencoding('utf-8')


# -----------------------------------------------------------------------------
# main process of: mode=normal
# -----------------------------------------------------------------------------
def processNormal():
    is_valid, data_type = conf.checkDataTypeNormal()
    if not is_valid:
        print 'Error: please confirm classification type of data-sets.'
        conf.printUsage()
        exit()
        
    dev_data  = conf.getDevData()
    test_data = conf.getTestData()

    if data_type == 'bc':
        print 'Data-Type: \"binay class classification\"'
        if conf.algorithm == 'without_classias':
            per_bc.train( dev_data )
            per_bc.test( test_data )
        else:
            cls.train( dev_data )
            cls.test( test_data )
    else:
        print 'Data-Type: \"multi class classification\"'

        if conf.algorithm == 'pegasos.hinge' or \
               conf.algorithm == 'truncated_gradient.hinge':
            print 'Error: %s is binary classification only.' % conf.algorithm
            exit()
        
        if conf.algorithm == 'without_classias':
            per_mc.train( dev_data )
            per_bc.test( test_data )
        else:
            cls.train( dev_data )
            cls.test( test_data )


# -----------------------------------------------------------------------------
# main process of: mode=cross
# -----------------------------------------------------------------------------
def processCross():
    is_valid, data_type = conf.checkDataTypeCloss()
    if not is_valid:
        print 'Error: development-dataset is invalid.'
        conf.printUsage()
        exit()

    if data_type == 'bc':
        print 'Data-Type: \"binay class classification\"'
        if conf.algorithm == 'without_classias':
            per_bc.crossvalidate()
        else:
            cls.crossvalidate()
    else:
        print 'Data-Type: \"multi class classification\"'
        
        if conf.algorithm == 'pegasos.hinge' or \
               conf.algorithm == 'truncated_gradient.hinge':
            print 'Error: %s is binary classification only.' % conf.algorithm
            exit()
            
        if conf.algorithm == 'without_classias':
            per_mc.crossvalidate()
        else:
            cls.crossvalidate();


# -----------------------------------------------------------------------------
# main function
# -----------------------------------------------------------------------------
def main():
    try:
        conf.getopts()
        
    except:
        print 'Command Error!!'
        conf.printUsage()
        exit()

    conf.makeLogDirectory()

    if not conf.hasSetFilenameOfDataSet():
        print 'Error: please input filenames of data-sets.'
        conf.printUsage()
        exit()

    if conf.mode == 'normal':
        processNormal()
    elif conf.mode == 'cross':
        processCross()

    
if(__name__=='__main__'):
    main()
